//
//  SRSportHelper.m
//  SportRing
//
//  Created by mykoma on 14-6-14.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRHelper.h"
#import "CRToast.h"

@implementation SRHelper

+(float)distanceMeterWithHeightMeter:(float)height steps:(float)steps
{
    float distance = (float)(height * steps/2.0 /2.8 /1000.0);
    return distance;
}

+(float)caloriesWithHeightMeter:(float)height weight:(float)weight steps:(float)steps
{
    float calorie = weight * height /560.0 * steps/2.0;
    return calorie;
}

+ (NSInteger)getHoursFromTime:(NSInteger)time
{
    
    NSInteger hour = time / 60;
    return hour;
    
}

+ (NSInteger)getMinutesFromTime:(NSInteger)time
{
    
    NSInteger minute = time % 60;
    
    return minute;
}

+(void)showToast:(NSString *)toast
         subText:(NSString *)subText
           force:(BOOL)force
 completionBlock:(void (^)(void))completion
{
    force ? [CRToastManager dismissAllNotifications:YES] : nil;
    NSDictionary *dict = @{
                           kCRToastTextKey : toast,
                           kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                           kCRToastBackgroundColorKey : [UIColor redColor],
                           kCRToastAnimationInTypeKey : @(CRToastAnimationTypeGravity),
                           kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeGravity),
                           kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionTop),
                           kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionBottom),
                           kCRToastUnderStatusBarKey : @(YES),
                           kCRToastNotificationTypeKey : @(CRToastTypeNavigationBar),
                           kCRToastTimeIntervalKey : @(2),
                           kCRToastInteractionRespondersKey : @[[CRToastInteractionResponder interactionResponderWithInteractionType:CRToastInteractionTypeTap
                                                                                                                automaticallyDismiss:YES
                                                                                                                               block:^(CRToastInteractionType interactionType){}]]
                           };
    NSMutableDictionary * options = [NSMutableDictionary dictionaryWithDictionary:dict];
    subText.length > 0 ? options[kCRToastSubtitleTextKey] = subText : nil;
    [CRToastManager showNotificationWithOptions:options
                                completionBlock:^{
                                    completion();
                                }];
}

+ (NSMutableAttributedString *)attString:(NSString *)str
                              fromString:(NSMutableArray *)fromStrings
                                toString:(NSMutableArray *)toStrings
                                fontSize:(NSMutableArray *)fonts
                                   color:(NSMutableArray *)colors
{
    
    __block NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
    
    [fromStrings enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        UIColor *color = [colors objectAtIndex:idx];
        
        UIFont *font = [fonts objectAtIndex:idx];

        NSDictionary *ats = @{
                              NSForegroundColorAttributeName: color,
                              NSFontAttributeName : font,
                              };
        
        NSString *fromString = obj;
        NSString *toString = [toStrings objectAtIndex:idx];
        
        NSRange range1;
        NSRange range = [str rangeOfString:toString];
        
        if ([fromString isEqualToString:@""])
        {
            range1 = NSMakeRange(0, 0);
            [attriString addAttributes:ats range:NSMakeRange(range1.location+range1.length, range.location)];

        }
        else
        {
            range1 = [str rangeOfString:fromString];
            [attriString addAttributes:ats range:NSMakeRange(range1.location+range1.length, range.location-range1.location-range1.length)];

        }
        
    }];
    

    return attriString;
}

+ (NSString *)timeFromDaySection:(int)section

{
    NSString *time = nil;
    
    int start = 2;
    int separte = 2;
    
    if (section >11)
    {
        time = [NSString stringWithFormat:@"%d:00",24];
    }
    else
    {
        start = start + separte *section;
        time = [NSString stringWithFormat:@"%d:00",start];

    }
    
    return time;
}



@end
